package com.zl.smartmall.library;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.listener.GetAddresssFromServerListener;
import com.zl.smartmall.library.listener.GetAllAddressesListener;
import com.zl.smartmall.library.listener.RequestCode;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;

import android.content.Context;

/**
 * 收货人地址管理相关操作接口
 * @author 陈希然
 *
 */
public class IAddress {

	private static final String TAG = IAddress.class.getSimpleName();
	private static IAddress mInstance;
	private static Lock mLock;
	private Context mContext;
	
	static{
		mLock = new ReentrantLock();
	}
	
	private IAddress(Context context) {
		mContext = context;
	}
	
	public static IAddress getInstance() {
		return mInstance;
	}
	
	public static void init(Context context) {
		if(mInstance == null){
    		mLock.lock();
    		try{
				if(mInstance == null){
					mInstance = new IAddress(context);
				}
    		}finally{
    			mLock.unlock();
    		}
    	}
	}
	
	/**
	 * 获取所有收货地址
	 * @param getAllAddressesListener 回调监听器
	 */
	public void getAllAddresses(GetAllAddressesListener getAllAddressesListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			getAllAddressesListener.onNoNetWork();
		
		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_ALL_ADDRESSES, getAllAddressesListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_GET_ALL_ADDRESSES);
		}
	}
	
	public void getProvinces(int reqType,int currentPid,GetAddresssFromServerListener listener){
		listener.setRequestCode(RequestCode.FIRST);
		if(!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_NEW_ADDRESS+"?type=1", listener);
			CrashHandler.d(TAG, "url = " + Constants.URL_NEW_ADDRESS);
		}
	}
	public void getCities(int reqType,int mCurrentPid,GetAddresssFromServerListener listener){
		listener.setmReqType(reqType);
		listener.setmCurrentPid(mCurrentPid);
		listener.setRequestCode(RequestCode.PULL);
		if(!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			String url = Constants.URL_NEW_ADDRESS+"?type=2&pid=" + mCurrentPid;
			SingtonAsyncHttpClient.getInstance().get(url, listener);
			CrashHandler.d(TAG, "url = " + Constants.URL_NEW_ADDRESS+"?type=2&pid=" + mCurrentPid);
		}
	}
	public void getCounTies(int reqType,int mCurrentPid,GetAddresssFromServerListener listener){
		listener.setmReqType(reqType);
		listener.setmCurrentPid(mCurrentPid);
		listener.setRequestCode(RequestCode.SCROLL);
		if(!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();
		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			String url = Constants.URL_NEW_ADDRESS+"?type=2&pid=" + mCurrentPid;
			SingtonAsyncHttpClient.getInstance().get(url, listener);
			CrashHandler.d(TAG, "url = " + Constants.URL_NEW_ADDRESS+"?type=2&pid=" + mCurrentPid);
		}
	}
}
